using System;
using System.IO;
using System.Linq;
using mock_ehr_lib.Model;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace mock_ehr_lib.Data
{
    public class JsonUserRepository : IUserRepository
    {
        private IEnumerable<User> _users;

        public JsonUserRepository(string filename)
        {
            initialize(filename);
        }

        void initialize(string filename)
        {
            if (!File.Exists(filename)) throw new FileNotFoundException("User JSON repository file not found.");
            var jsonFile = File.ReadAllText(filename);
            var patientsJobject = JObject.Parse(jsonFile);
            _users = patientsJobject["Users"].ToObject<User[]>();
        }

        public IEnumerable<User> GetAllUsers()
        {
            return _users;
        }
    }
}